package com.rht.textsize

import android.content.res.Resources
import android.view.View
import com.mxin.mvvm.BaseMvvmActivity
import com.mxin.mvvm.viewmodel.BaseViewModel
import com.mxin.util.ConvertUtils
import com.rht.textsize.databinding.ActivityTextSizeBinding

class TextSizeActivity : BaseMvvmActivity<ActivityTextSizeBinding,BaseViewModel>() {


    override fun showLoadingView(): View? {
       return null
    }

    override fun initData() {
        viewBinding.fontsizeview.setChangeCallbackListener {
            val dimension = resources.getDimensionPixelSize(R.dimen.sp_stander)
            //根据position 获取字体倍数
            val fontSizeScale = (0.875f + 0.125f * it)
            //放大后的sp单位
            val v = fontSizeScale * ConvertUtils.px2sp(dimension.toFloat())
            //改变当前页面大小
            changeTextSize(v)

        }
    }

    override fun initView() {

    }

    /**
     * 改变textsize 大小
     */
    private fun changeTextSize(dimension:Float) {
        viewBinding.tvContent.textSize = dimension;
    }

    override fun getResources(): Resources {
        val res =super.getResources()
        val config = res.configuration
        //fontSizeScale = (float) SPUtils.get(this, Constants.SP_FontScale, 0.0f);
//        if(fontSizeScale>0.5){//防止第一次获取SP时得到默认值0
//            config.fontScale= fontSizeScale;//设置正常字体大小的倍数
//        }
        res.updateConfiguration(config,res.getDisplayMetrics());
        return res;
//        return createConfigurationContext(config).resources

    }

    override fun onLoadRetry() {

    }
}